package com.menker.app.ui.welcome;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.menker.app.Constants;
import com.menker.app.MyApp;
import com.menker.app.R;
import com.menker.app.common.SharedPreferencesHelper;
import com.menker.app.common.Utils;
import com.menker.app.di.DaggerScope;
import com.menker.app.di.Injector;
import com.menker.app.ui.main.MainActivity;
import com.menker.app.ui.main.MainActivity_;
import com.menker.app.ui.splash.SplashActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import pl.droidsonroids.gif.GifImageView;

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(WelcomeActivity.class)
@EActivity(R.layout.activity_welcome)
public class WelcomeActivity extends Activity implements ViewPager.OnPageChangeListener {
    @ViewById(R.id.viewpager_ad)
    ViewPager mViewPager;
    @ViewById(R.id.welcome_btn_next)
    ImageButton mOkButton;

    @Inject
    SharedPreferencesHelper mSharedPrefHelper;

    // 引导图片资源
    private static final int[] pics = {
            R.drawable.welcome_img_1,
            R.drawable.welcome_img_2,
            R.drawable.welcome_img_3,
            R.drawable.welcome_img_4,
    };

    @AfterInject
    void initDI() {
        Injector.initWelcomeActivityComponent().inject(this);

        mSharedPrefHelper.setData(Constants.API.KEY_SPLASH,true);

        if (mSharedPrefHelper.getBoolean("firstStart", true)) {
            mSharedPrefHelper.setData("firstStart", false);
        } else {
            toLogin();
        }
    }

    @AfterViews
    void initViews() {
        List<View> views = new ArrayList<>();

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        // 初始化引导图片
        for (int resId : pics) {
            GifImageView iv = new GifImageView(this);
            iv.setLayoutParams(mParams);
            iv.setBackgroundResource(resId);
            views.add(iv);
        }

        // 初始化Adapter
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(views);
        mViewPager = (ViewPager) findViewById(R.id.viewpager_ad);

        mViewPager.setAdapter(viewPagerAdapter);
        // 绑定回调
        mViewPager.addOnPageChangeListener(this);

    }
/*
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);

        List<View> views = new ArrayList<>();

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        // 初始化引导图片
        for (int resId : pics) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setImageResource(resId);
            views.add(iv);
        }

        // 初始化Adapter
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(views);
        mViewPager = (ViewPager) findViewById(R.id.viewpager_ad);

        mViewPager.setAdapter(viewPagerAdapter);
        // 绑定回调
        mViewPager.addOnPageChangeListener(this);

        // 初始化底部小圆点
        initDots();

        // 初始化"马上体验"按钮
        mOkButton = (ImageButton) findViewById(R.id.btn_ad_ok);
        mOkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(adActivity.this, LoginActivity.class);
                adActivity.this.startActivity(intent);
                finish();
            }
        });
    }
*/

    @Click(R.id.welcome_btn_next)
    void toLogin() {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity_.class);
        startActivity(intent);
        finish();
    }

    /**
     * 当前页面被滑动时调用
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    /**
     * 当新的页面被选中时调用
     */
    @Override
    public void onPageSelected(int position) {

        if(position == pics.length-1){
            mOkButton.setVisibility(View.VISIBLE);
        }else{
            mOkButton.setVisibility(View.GONE);
        }
    }

    /**
     * 当滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int state) {}

    /**
     * 设置当前的引导页
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }

        mViewPager.setCurrentItem(position);
    }

}
